#include <stdio.h>
#include <stdlib.h>

void dynamic_array();
void dynamic_array_2();
void dynamic_array_3();
void dynamic_array_4();
void dynamic_array_5();

int main(void)
{
    dynamic_array();
    printf("------\n");
    dynamic_array_2();
    printf("------\n");
    dynamic_array_3();
    printf("------\n");
    dynamic_array_4();
    printf("------\n");
    dynamic_array_5();
    return 0;
}

void dynamic_array()
{
    // 初始化
    int(*ptr)[3][2] = calloc(3 * 2, sizeof(int));
    // 赋值
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 2; ++j)
        {
            (*ptr)[i][j] = j;
            printf("%p\n", &((*ptr)[i][j])); // 等同于下句
            // printf("%p\n", *(*ptr + i) + j);
        }
    }
    // 打印
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 2; ++j)
        {
            printf("%d ", (*ptr)[i][j]);
        }
        printf("\n");
    }
    free(ptr);
}

void dynamic_array_2()
{
    // 初始化
    int(*ptr)[2] = calloc(3 * 2, sizeof(int));
    // 赋值
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 2; ++j)
        {
            *(ptr[i] + j) = j; // 等同于下面的
            //*(*(ptr + i) + j) = j;
            printf("%p\n", &(*(ptr[i] + j)));
        }
    }
    // 打印
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 2; ++j)
        {
            printf("%d ", *(ptr[i] + j));
        }
        printf("\n");
    }
    free(ptr);
}

void dynamic_array_3()
{
    // 初始化
    int *a = calloc(2 * 3, sizeof(int));

    // 赋值
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 2; ++j)
        {
            *(a + 2 * i + j) = j;
            printf("%p\n", &(*(a + 2 * i + j)));
        }
    }
    // 打印
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 2; ++j)
        {
            printf("%d ", *(a + 2 * i + j));
        }
        printf("\n");
    }
    free(a);
}

void dynamic_array_4()
{
    // 初始化
    int **ptr = calloc(3, sizeof(int *));
    for (int i = 0; i < 3; ++i)
    {
        *(ptr + i) = calloc(2, sizeof(int));
    }
    // 赋值
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 2; ++j)
        {
            *(*(ptr + i) + j) = j;
            printf("%p\n", &(*(*(ptr + i) + j)));
        }
    }
    // 打印
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 2; ++j)
        {
            printf("%d ", *(*(ptr + i) + j));
        }
        printf("\n");
    }

    for (int i = 0; i < 3; ++i)
    {
        free(*(ptr + i));
    }
    free(ptr);
}

void dynamic_array_5()
{
    // 初始化
    int **ptr = calloc(3, sizeof(int *));
    *ptr = calloc(3 * 2, sizeof(int));
    for (int i = 1; i < 3; ++i)
    {
        *(ptr + i) = *ptr + i * 2;
    }
    // 赋值
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 2; ++j)
        {
            *(*(ptr + i) + j) = j;
            printf("%p\n", &(*(*(ptr + i) + j)));
        }
    }
    // 打印
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 2; ++j)
        {
            printf("%d", *(*(ptr + i) + j));
        }
        printf("\n");
    }
}